﻿<%@ WebHandler Language="C#" Class="test1" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;

public class test1 : AjaxHandlerBase
{

    public override void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string optype = context.Request["op"];
        switch (optype.ToLower())
        {
            case "getlist":
                GetList(context);       //查询数据列表
                break;
            case "getjsongrid":
                GetJsonGrid(context);
                break;
        }
    }



    //查询数据列表
    private void GetList(HttpContext context)
    {
        try
        {
            //参数列表
            int iPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            int iPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数

            //order文
            string strOrder = "t_Business_EmpSetting.DepartId desc,t_Business_EmpSetting.IsBelong desc,t_Business_EmpSetting.usercode asc";
            //查询表格
            string tablename = "t_Business_EmpSetting";
            //select文
            string TableFieldList = "RecordId,UserCode,UserName,Company,DepartName,IsBelong,IsControl,SearchContractArchive,EditContractArchive,CancelContractArchive,SearchContractApply,EditContractApply";
            string FieldList = "RecordId,UserCode,UserName,Company,DepartName,IsBelong,IsControl,SearchContractArchive,EditContractArchive,CancelContractArchive,SearchContractApply,EditContractApply";


            //权限
            string strwhere = "";

            Guid cityId = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
            Guid companyId = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
            Guid areaId = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaId"]);
            Guid departId = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);
            Guid userId = (string.IsNullOrEmpty(context.Request["userid"])) ? Guid.Empty : new Guid(context.Request["userid"]);
            string usercode = context.Request["usercode"];

            //判断权限范围/////////////////////////////////////////////
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_All))
            {
                strwhere = " (1=1) ";
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_City))
            {
                strwhere = string.Format(" (CityId='{0}') ", CurrentUserInfo.CityId.ToString());
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_Company))
            {
                strwhere = string.Format(" (CompanyId='{0}') ", CurrentUserInfo.CompanyId.ToString());
            }
            else
                strwhere = " (1=0) ";

            if (departId != Guid.Empty)
                strwhere += string.Format(" and DepartId='{0}' ", departId.ToString());
            else if (areaId != Guid.Empty)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, areaId);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(areaId);
                    if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
                }

                if (item == null) context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", "获取商圈或分类数据丢失"));

                strwhere += string.Format(" AND (DepartId in (select departId from dbo.v_Hr_DepartmentInfo where DepartSrc like '{0}%')) ", item.DepartSrc);
            }
            else if (companyId != Guid.Empty)
                strwhere += string.Format(" and CompanyId='{0}' ", companyId.ToString());
            else if (cityId != Guid.Empty)
                strwhere += string.Format(" and CityId='{0}' ", cityId.ToString());
            else
                strwhere += string.Format(" and CompanyId='{0}' ", CurrentUserInfo.CompanyId.ToString());
            if (userId != Guid.Empty)
                strwhere += string.Format(" and UserId='{0}' ", userId.ToString());
            if (!string.IsNullOrEmpty(usercode))
                strwhere += string.Format(" and usercode='{0}' ", usercode);


            DataSet list = Chenxin.Lib.Business.DAL.t_Business_EmpSetting.GetList(iPageIndex, iPageSize, strOrder, tablename, strwhere, FieldList, TableFieldList, true);

            int count = (int)list.Tables[1].Rows[0][0];
            string strHtml = "";

            if (!GetKeyListHtml(list, out strHtml))
            {
                context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", "生成HTML错误:" + strHtml));
            }
            list.Dispose();
            list = null;
            //string head = "{UserCode: {head: \"工号\",width: 100,sortable:true},UserName: {head: \"姓名\",width: 120,sortable:true},Company: {head: \"公司\",width: 80,sortable:true},DepartName: {head: \"部门\",width: 80,sortable:true},IsBelong: {width: 50,head: \"隶属\"},IsControl: {width: 50, head: \"管辖\"},SearchContractArchive: {width: 80, head: \"查询报备\"},EditContractArchive: {width: 80,head: \"编辑报备\"},CancelContractArchive: {width: 80, head: \"作废报备\"},SearchContractApply: {width: 190,head: \"查询业绩申请\"},EditContractApply: {width: 90, head: \"修改业绩申请\"}}";
            //context.Response.Write(string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',html:'{2}',head:{3}}}}}", "成功", count, strHtml, head));
            context.Response.Write(string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',html:'{2}'}}}}", "成功", count, strHtml));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message));
        }
    }


    private bool GetKeyListHtml(System.Data.DataSet dslist, out string strHtml)
    {
        try
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (System.Data.DataRow row in dslist.Tables[0].Rows)
            {
                sb.AppendFormat("<dl id=\"{0}\" class=\"x-grid-row\">", row["RecordId"].ToString());
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "UserCode", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "UserName", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "Company", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "DepartName", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "IsBelong", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "IsControl", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "SearchContractArchive", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "EditContractArchive", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "CancelContractArchive", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "SearchContractApply", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "EditContractApply", "").ToLower() == "true" ? "√" : "");
                sb.Append("</dl>");
            }
            strHtml = sb.ToString();
            return true;
        }
        catch (Exception ex)
        {
            strHtml = ex.Message;
            return false;
        }
    }

    
    private void GetJsonGrid(HttpContext context)
    {
        try
        {
            //参数列表
            int iPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            int iPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数

            //order文
            string strOrder = "t_Business_EmpSetting.DepartId desc,t_Business_EmpSetting.IsBelong desc,t_Business_EmpSetting.usercode asc";
            //查询表格
            string tablename = "t_Business_EmpSetting";
            //select文
            string TableFieldList = "RecordId,UserCode,UserName,Company,DepartName,IsBelong,IsControl,SearchContractArchive,EditContractArchive,CancelContractArchive,SearchContractApply,EditContractApply";
            string FieldList = "RecordId,UserCode,UserName,Company,DepartName,IsBelong,IsControl,SearchContractArchive,EditContractArchive,CancelContractArchive,SearchContractApply,EditContractApply";


            //权限
            string strwhere = "";

            Guid cityId = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
            Guid companyId = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
            Guid areaId = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaId"]);
            Guid departId = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);
            Guid userId = (string.IsNullOrEmpty(context.Request["userid"])) ? Guid.Empty : new Guid(context.Request["userid"]);
            string usercode = context.Request["usercode"];

            //判断权限范围/////////////////////////////////////////////
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_All))
            {
                strwhere = " (1=1) ";
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_City))
            {
                strwhere = string.Format(" (CityId='{0}') ", CurrentUserInfo.CityId.ToString());
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_Company))
            {
                strwhere = string.Format(" (CompanyId='{0}') ", CurrentUserInfo.CompanyId.ToString());
            }
            else
                strwhere = " (1=0) ";

            if (departId != Guid.Empty)
                strwhere += string.Format(" and DepartId='{0}' ", departId.ToString());
            else if (areaId != Guid.Empty)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, areaId);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(areaId);
                    if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
                }

                if (item == null) context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", "获取商圈或分类数据丢失"));

                strwhere += string.Format(" AND (DepartId in (select departId from dbo.v_Hr_DepartmentInfo where DepartSrc like '{0}%')) ", item.DepartSrc);
            }
            else if (companyId != Guid.Empty)
                strwhere += string.Format(" and CompanyId='{0}' ", companyId.ToString());
            else if (cityId != Guid.Empty)
                strwhere += string.Format(" and CityId='{0}' ", cityId.ToString());
            else
                strwhere += string.Format(" and CompanyId='{0}' ", CurrentUserInfo.CompanyId.ToString());
            if (userId != Guid.Empty)
                strwhere += string.Format(" and UserId='{0}' ", userId.ToString());
            if (!string.IsNullOrEmpty(usercode))
                strwhere += string.Format(" and usercode='{0}' ", usercode);


            DataSet list = Chenxin.Lib.Business.DAL.t_Business_EmpSetting.GetList(iPageIndex, iPageSize, strOrder, tablename, strwhere, FieldList, TableFieldList, true);

            int count = (int)list.Tables[1].Rows[0][0];

            context.Response.Write(string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',{2}}}}}", "成功", count, JsonHelp.Convert2Json(list.Tables[0])));
        }
        catch
        {

        }
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}